﻿<%@ Page Title="" Language="C#" MasterPageFile="~/SIPO.master" AutoEventWireup="false" CodeFile="recDebtorlist.aspx.cs" Inherits="SIPO.Web.Lists.Administration.Catalog.recDebtorlist" %>


<asp:Content ID="ctnBody" ContentPlaceHolderID="body" runat="server">
    <link type="text/css" rel="stylesheet" href="<%= ResolveUrl("~/styles/ui.jqgrid.css") %>" />
    <script type="text/javascript" src="<%= ResolveUrl("~/scripts/i18n/grid.locale-id.js") %>"></script>
    <script type="text/javascript" src="<%= ResolveUrl("~/scripts/jquery.jqGrid.min.js") %>"></script>
       
    <div id="btn">
       <button id="btnAdd" type="button" onclick="OpenForm('add');">Tambah</button>
        <button id="btnEdit" type="button" onclick="OpenForm('edit');">Ubah</button>
        <button id="btnDelete" type="button" onclick="OpenForm('delete');">Hapus</button>
    </div>
    <br />
    <br />
    <table id="table" class="scroll" cellpadding="0" cellspacing="0">
    </table>
    <div id="pager" class="scroll">
    </div>
    <br />
    <div id="dialog" title="Default dialog">
        <asp:HiddenField ID="HfID" runat="server" />
    </div>

     <script type="text/javascript">
         var id = $('#<%= HfID.ClientID %>').val();
         var Edit = '<%= Request.Params["Edit"] %>';
        
      if (Edit.toString() == 'true') {
            OpenForma();
      }
        function OpenForma() {
            $('#table').hide();
            $('#btn').hide();
            var formUrla = '<%= ResolveUrl("~/forms/administration/catalog/recDebtorform.aspx") %>';
            $('#dialog').load(formUrla +'?edit='+ Edit+ '&id=' + id + '&menuId=' + '<%= base.MenuID %>').dialog({ autoOpen: false, modal: true,height: 550, width: 500, title: 'Ubah Perusahaan' });
            $("#dialog").dialog('open');
            return false;
        }
         $("#btnAdd").button({ icons: { primary: "ui-icon-plus"} });
         $("#btnEdit").button({ icons: { primary: "ui-icon-pencil"} });
         $("#btnDelete").button({ icons: { primary: "ui-icon-trash"} });

    </script>
    
    <script type="text/javascript">
//        $(document).ready(function () {


          if (Edit.toString() != 'true') {
            var handlerUrl = '<%= ResolveUrl("~/AjaxHandler/Administration/Catalog/recDebtorhandler.ashx") %>';

            $('#table').jqGrid({
                url: handlerUrl + '?method=getdata',
                datatype: 'json',
                colNames: ['ID', 'Kode Perusahaan', 'Nama Perusahaan', 'NPWP', 'Alamat 1', 'Alamat 2', 'Alamat 3', 'Kode Pos', 'Wilayah', 'Kota', 'Email', 'Telepon', 'Fax', 'Website', 'Aktif', 'Sektor'],
                colModel: [
           		{ name: 'DebtorID', key: true, hidden: true, index: 'DEBTORID', width: 60 },
           		{ name: 'DebtorCode', index: 'DEBTORCODE', width: 280, searchoptions: { sopt: ['eq', 'cn']} },
                { name: 'DebtorName', index: 'DEBTORNAME', width: 350, searchoptions: { sopt: ['eq', 'cn']} },
                { name: 'NPWP', index: 'NPWP', width: 200, searchoptions: { sopt: ['eq', 'cn']} },
                { name: 'Address1', index: 'ADDRESS1', width: 200, searchoptions: { sopt: ['eq', 'cn']} },
                { name: 'Address2', index: 'ADDRESS2', width: 200, searchoptions: { sopt: ['eq', 'cn']} },
                { name: 'Address3', index: 'ADDRESS3', width: 200, searchoptions: { sopt: ['eq', 'cn']} },
                { name: 'ZipCode', index: 'ZIPCODE', width: 200, searchoptions: { sopt: ['eq', 'cn']} },
                { name: 'recRegion.RegionName', index: 'recRegion.REGIONNAME', width: 200, searchoptions: { sopt: ['eq', 'cn']} },
                { name: 'recCity.CityName', index: 'recCity.CITYNAME', width: 200, searchoptions: { sopt: ['eq', 'cn']} },
                { name: 'Email', index: 'EMAIL', width: 200, searchoptions: { sopt: ['eq', 'cn']} },
                { name: 'Phone', index: 'PHONE', width: 200, searchoptions: { sopt: ['eq', 'cn']} },
                { name: 'Fax', index: 'FAX', width: 200, searchoptions: { sopt: ['eq', 'cn']} },
                { name: 'Website', index: 'WEBSITE', width: 200, searchoptions: { sopt: ['eq', 'cn']} },
                { name: 'IsActive', index: 'ISACTIVE', width: 50, align: 'Center', formatter: 'checkbox', formatoptions: { disabled: true }, searchoptions: { sopt: ['eq']} },
           	    { name: 'sector', index: 'SECTOR', align: 'center', search: false, sortable: false, width: 100 },
            ],
                rowNum: 10,
                id: 'DebtorID',
                multiselect: true,
                rowList: [5, 10, 15, 20],
                pager: '#pager',
                sortname: 'DEBTORCODE',
                sortorder: "asc",
                viewrecords: true,
                jsonReader: { repeatitems: false },
                width: 1200,
                height: '100%',
                caption: 'Perusahaan',
                ajaxGridOptions: { cache: false },
                gridComplete: function () {
                    var ids = $('#table').jqGrid('getDataIDs');
                    for (var i = 0; i < ids.length; i++) {
                        var _id = $('#table').jqGrid('getCell', ids[i], 'DebtorID');
                        var _name = $('#table').jqGrid('getCell', ids[i], 'DebtorName');

                        sectorId = "<a href='javascript:void(0);' class='dialog_link ui-corner-all' title='Sektor' onclick=\"onclickButtonSector('" + _id + "','" + _name + "');\"><span class='ui-icon ui-icon-newwin'></span>...</a>";

                        $('#table').jqGrid('setRowData', ids[i], { sector: sectorId });
                    }
                }
            });

            $("#table").jqGrid('navGrid', '#pager', { edit: false, add: false, del: false, searchtext: 'Search', refreshtext: 'Refresh' },
        {}, {}, {}, { closeOnEscape: true, multipleSearch: true, closeAfterSearch: true });
        
            function onclickButtonSector(id, name) {
                $('#table').jqGrid('resetSelection');

                var urlList = '<%= ResolveUrl("~/lists/administration/catalog/recDebtorSector.aspx") %>';
                $('#dialog').load(urlList + '?' + 'method=getdata&DebtorID=' + id).dialog({ autoOpen: false, modal: true, width: 630, height: 440, title: 'Sektor untuk ' + name });
                $('#dialog').dialog('open');
            }
        } 
  
   function OpenForm(ctl) {

       var formUrl = '<%= ResolveUrl("~/forms/administration/catalog/recDebtorform.aspx") %>';

       var grid = $('#table');
       var selIds = grid.jqGrid('getGridParam', 'selarrrow'),
                selText = [];
       $.each(selIds, function () {
           selText.push(grid.jqGrid('getCell', this, 'DebtorID'));
       });

       if (ctl == 'edit') {
           if (selIds.length == 0) {
               alert('Pilih data yang mau di ubah !');
           }
           else if (selIds.length > 1) {
               alert('Pilih satu record untuk ubah data.');
           }
           else {
               $('#dialog').load(formUrl + '?id=' + selText + '&menuId=' + '<%= base.MenuID %>').dialog({ autoOpen: false, modal: true, width: 500, title: 'Ubah Perusahaan' });
               $("#dialog").dialog('open');
           }
       }
       else if (ctl == 'add') {
           $('#dialog').load(formUrl + '?menuId=' + '<%= base.MenuID %>').dialog({ autoOpen: false, modal: true, width: 500, title: 'Tambah Perusahaan' });
           $("#dialog").dialog('open');
       }
       else {
           if (selIds.length == 0) {
               alert('Pilih data yang mau di hapus!');
           }
           else {
               var result = window.confirm('Benar data akan di hapus ?');
               if (result) {

                   var objrecDebtor = new Object();
                   objrecDebtor.DebtorID = encodeURIComponent(selText);

                   jsonData = JSON.stringify(objrecDebtor);

                   CallAjax('<%= ResolveUrl("~/ajaxhandler/administration/catalog/recDebtorhandler.ashx") %>', "?method=delete", jsonData);

                   setTimeout("$('#table').trigger('reloadGrid');", 3000);

               }
           }
       }
   }
         
//             });
        
    </script>

</asp:Content>

